﻿ 
using HBase;
using hCommand;
using Net.http;
using Persistence;
using System;
using System.Collections.Generic; 

namespace WebApi {
    //[RoutePrefix("api")]
    public class LoginController :BaseApiController {
        ISysAuthorityServer authorityServer = WMSDalFactray.getDal<ISysAuthorityServer>();


        [HttpPost, Route("login"), ControlName("登录操作")]
        public BaseResult login([FromBody] UserDto user) {
            List<SysUser> listByName = authorityServer.GetUser(user.account);
            if( listByName == null || listByName.Count == 0 ) {
                return BaseResult.Error("账号不存在！");
            }
            List<SysUser> listByNamePwd = authorityServer.GetUser(user.account ,HUtil.MD5Encrypt(user.password));
            if( listByNamePwd == null || listByNamePwd.Count == 0 ) {
                return BaseResult.Error("密码不正确！");
            }
            if( listByName[0].userstatus == 0 ) {
                return BaseResult.Error("账号已锁定！");
            }
            AuthenticatioToken token = new AuthenticatioToken();
            String name = user.account;

            token.token = byteMargin((DateTime.Now.Ticks + "") ,name);
            CookieHelper.addtoken(token.token);
            return BaseResult.Ok(token);
        }

        String byteMargin(String str1 ,String str2) {

            str1 = str1.Substring(0 ,15);
            byte[] time = System.Text.Encoding.Default.GetBytes(str1);

            byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(str2);

            byte[] res = new byte[time.Length + byteArray.Length];
            int indexer1 = 0;
            int indexer2 = 0;
            for( int i = 0;i < res.Length;i++ ) {
                if( i % 2 == 0 ) {//0
                    if( indexer1 == time.Length ) {//时间结束了
                        res[i] = byteArray[indexer2++];
                    } else {
                        res[i] = time[indexer1++];
                    }
                } else {
                    if( indexer2 == byteArray.Length ) {//用户名结束
                        res[i] = time[indexer1++];
                    } else {
                        res[i] = byteArray[indexer2++];
                    }
                }
            }
            string pic = Convert.ToBase64String(res);
            return pic;
        }



        [HttpPost, Route("logout"), ControlName("登出操作")]
        public BaseResult logout([FromBody] UserDto user) {

            return BaseResult.Ok("");
        }








    }


}
